package com.lbs.jiepiaisi.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.bean.DeptsEntity;
import java.util.List;


public class DeptListAdapter extends RecyclerView.Adapter {

    private int selectIndex;
    List<DeptsEntity.Result> deptsList;

    //
    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }


    // DeptListAdapter的构造方法
    public DeptListAdapter(List<DeptsEntity.Result> deptsList) {
        this.deptsList = deptsList;
    }


    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int index);
    }

    private OnItemClickListener listener;

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_view,viewGroup,false );
        DeptListViewHolder viewHolder = new DeptListViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        DeptsEntity.Result r = deptsList.get(i);
        ((DeptListViewHolder)viewHolder).textView.setText(r.getDeptName());
        ((DeptListViewHolder)viewHolder).textView.setPadding(30 * (r.getLevel() - 1),0,0,0);

        if (i == getSelectIndex()){
            ((DeptListViewHolder)viewHolder).markView.setVisibility(View.VISIBLE);
            viewHolder.itemView.setBackgroundColor(Color.parseColor("#D0D0D0"));
        } else {
            ((DeptListViewHolder)viewHolder).markView.setVisibility(View.INVISIBLE);
            viewHolder.itemView.setBackgroundColor(Color.parseColor("#F2F2F2"));
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(i);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return deptsList.size();
    }


    private class DeptListViewHolder extends RecyclerView.ViewHolder{

        private TextView textView;
        private ImageView markView;

        // 构造方法
        public DeptListViewHolder(@NonNull final View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_accont);
            markView = itemView.findViewById(R.id.iv_select_mark);
        }
    }
}
